package com.zxl.grocery.core.webmagic.pipeline;



import com.zxl.grocery.common.persistence.model.CrawlerNews;
import com.zxl.grocery.modular.crawler.service.ICrawlerNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * info:新闻
 * Created by Lzx on 2017/7/12.
 */
@Repository
public class NewsPipeline implements Pipeline {

    @Autowired
    protected ICrawlerNewsService iCrawlerNewsService;

    @Override
    public void process(ResultItems resultItems, Task task) {
        for (Map.Entry<String, Object> entry : resultItems.getAll().entrySet()) {
            if (entry.getKey().contains("news")) {
                CrawlerNews news=(CrawlerNews) entry.getValue();
                HashMap<String,Object> conditionMap = new HashMap<String,Object>();
                conditionMap.put("link",news.getLink());
                List<CrawlerNews> newsList = iCrawlerNewsService.selectByMap(conditionMap);

                if (newsList.size() < 1) {//检查链接是否已存在
                    news.setAuthor("zxl");
                    news.setSort(1);
                    news.setStatus(1);
                    news.setPlacedTop(0);
                    news.setRecommend(0);
                    news.setThumbUp(0);
                    news.setExplicitLink(1);
                    news.setCreateDate(new Date());
                    news.setModifyDate(new Date());
                    iCrawlerNewsService.insert(news);
                }
            }

        }
    }
}
